import User from "../../../../utils/user";
import msg from "../../../../utils/msg";
import { ExportExcelAll, exportExcelOne, getDutyCourseDetail } from "../../../../utils/request/api";
import { url } from "../../../../utils/request/route";
import {  sr_data } from "../util";
import { Course } from "../../../../pages/curriculum/interface";
var user = new User;
// pages/user/s_worker/chooseTime/chooseTime.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{} as {[time_id:string]:Array<sr_data>},//已弃用
    course:{} as Course,
    sign_type:[] as any,
    times:{} as {[time_id:string]:Array<string>},
    signRate:{} as {[time_id:string]:any},
    pickerIndex:0 as number,
    pickerArray:[] as string[],
    course_id:0 as number,
  },
  _getPickerArray(){
    let pickerArray:string[]=[];
    pickerArray.push('更改全课签到细节');
    let times:any=this.data.signRate;
    for(let key in times){
        let title = '第'+times[key].week+'周 星期'+times[key].c_day+' '+times[key].c_time+'节';
        pickerArray.push(title);
    }
    this.setData({
        pickerArray:pickerArray,
    });
  },
  bindPickerChange(e:any){
    let value=e.detail.value;//index
    let index:number = Number(value);
    if(index==NaN) return;
    else if(index==0){
        this.goToWork2('',true);
        return;
    }
    let i=1;
    for(let key in this.data.signRate){
        if(i<index)i++;
        else{
            this.goToWork2(key);
            return;
        }
    }
  },
  // goToWork(e:any){
  //   let time_id = e.currentTarget.dataset.index;
  //   let data = JSON.stringify(this.data.data[time_id]);
  //   wx.navigateTo({
  //     url:'/pages/user/s_worker/chooseTime/s_work/s_work'+'?data='+data+'&sign_type='+JSON.stringify(this.data.sign_type),
  //   })
  // },
  goToWork(e:any){
    let time_id = e.currentTarget.dataset.index;
    let data:any = this.data.signRate[time_id];
    data['course_id']=this.data.course_id;
    
    wx.navigateTo({
      url:'/pages/user/s_worker/chooseTime/s_work/s_work'+'?data='+JSON.stringify(data)+'&sign_type='+JSON.stringify(this.data.sign_type),
    })
  },
  //更改签到细节
  goToWork2(time_id:string,editall:boolean=false){
    let url='/pages/user/s_worker/chooseTime/s_work2/s_work2';
    if(editall){
      url+='?editAll='+true+'&data='+JSON.stringify(this.data.course);
    }
    else {
      let data = JSON.stringify(this.data.signRate[time_id].signDetail);
      url+='?data='+data;
    }
    wx.navigateTo({
      url:url,
    })
  },
  onClickLeft(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    msg.Loading('加载中');
    let self = this;
    getDutyCourseDetail({course_id:options.course_id,openid:user.openid})
    .then((res:any)=>{
      //self._getData2(res.data.data.sr_data);
      self.setData({
        course:res.data.data.courseInfo,
        sign_type:JSON.parse(options.sign_type),
        course_id:options.course_id,
        signRate:res.data.data.signRate,
      });
      this._getPickerArray();
      msg.hideLoading();
    });
  },
  //数据分析
  analysis(){
    wx.navigateTo({
        url:'./analysis/analysis?course='+JSON.stringify(this.data.course),
    })
  },
  download:async function(){
    await msg.confirm('确定导出全部签到数据吗','');
    msg.Loading('导出数据中');
    let course:any=this.data.course;
    await ExportExcelAll({data:{course_id:course.course_id},fileName:course.name});
    msg.hideLoading();
  },
  exportOne:async function(e:any){
    await msg.confirm('确定导出这一堂课的签到数据吗','');
    msg.Loading('导出数据中');
    let index = e.currentTarget.dataset.index;
    let data = this.data.signRate[index];
    let course:any = this.data.course;
    await exportExcelOne({data:{course_id:course.course_id,week:data.week,day:data.c_day,time:data.c_time},fileName:course.c_name});
    msg.hideLoading();
  },
  _getData2(sr_data:sr_data[]){
    let data2 = {} as {[time_id:string]:Array<sr_data>};
    let times={} as {[time_id:string]:Array<string>};
    sr_data.forEach((item,index)=>{
      let time_id = item.week+'-'+item.c_day+'-'+item.c_time;
      if(!data2[time_id]){
        data2[time_id]=[];
        let time = time_id.split('-');
        times[time_id]=time;
      }
      data2[time_id].push(item);
    });
    //
    let signRate=this.data.signRate;
    for(let time_id in data2){
        let needSign=0;
        let hasSign=0;
        data2[time_id].forEach((item)=>{
            if(item.type=='待签到'){ 
                needSign++;
            }
            else if(item.type=='正常签到'||item.type=='迟到'){
                needSign++;hasSign++;  
            }
        });
        signRate[time_id]=hasSign+'/'+needSign;
    }
    this.setData({
      data:data2,
      times:times,
      signRate:signRate,
    });
  },
  //导出excel

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})